package com.doys.myfirstapp;
import android.view.KeyEvent;

import java.util.Date;

public class ScanHelper {
    private static int TIMEOUT = 200;
    private static int nIdx = 0;
    private static int MAX_SIZE = 128;
    private static int[] keys = new int[MAX_SIZE];

    private static long nTimeLast = new Date().getTime();
    // ------------------------------------------------------------------------
    public static String analysisKeyEvent(KeyEvent event) {
        int keyCode;
        long duration;

        String resultString;
        // ------------------------------------------------
        try {
            duration = new Date().getTime() - nTimeLast;
            if (duration > TIMEOUT) {
                resetBuffer();
            }

            keyCode = event.getKeyCode();
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                resultString = getScanResult();
                resetBuffer();
                return resultString;
            }
            else {
                if (nIdx < MAX_SIZE) {
                    keys[nIdx++] = keyCode;
                }
                nTimeLast = new Date().getTime();
            }
        } catch (Exception e) {
            throw e;
        }
        return null;
    }

    private static void resetBuffer() {
        nTimeLast = new Date().getTime();
        nIdx = 0;
    }
    private static String getScanResult() {
        String resultString;
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < nIdx; i++) {
            builder.append(getInputCode(keys[i]));
        }
        resultString = builder.toString();

        return resultString;
    }

    // -- 转码 ------------------------------------------------------------------
    private static char getInputCode(int keyCode) {
        boolean caps = false;
        char ch;

        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
            ch = (char) ((caps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        }
        else {
            ch = keyValue(caps, keyCode);
        }
        return ch;
    }
    private static char keyValue(boolean caps, int keyCode) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_0:
                return caps ? ')' : '0';
            case KeyEvent.KEYCODE_1:
                return caps ? '!' : '1';
            case KeyEvent.KEYCODE_2:
                return caps ? '@' : '2';
            case KeyEvent.KEYCODE_3:
                return caps ? '#' : '3';
            case KeyEvent.KEYCODE_4:
                return caps ? '$' : '4';
            case KeyEvent.KEYCODE_5:
                return caps ? '%' : '5';
            case KeyEvent.KEYCODE_6:
                return caps ? '^' : '6';
            case KeyEvent.KEYCODE_7:
                return caps ? '&' : '7';
            case KeyEvent.KEYCODE_8:
                return caps ? '*' : '8';
            case KeyEvent.KEYCODE_9:
                return caps ? '(' : '9';
            case KeyEvent.KEYCODE_NUMPAD_SUBTRACT:
                return '-';
            case KeyEvent.KEYCODE_MINUS:
                return '_';
            case KeyEvent.KEYCODE_EQUALS:
                return '=';
            case KeyEvent.KEYCODE_NUMPAD_ADD:
                return '+';
            case KeyEvent.KEYCODE_GRAVE:
                return caps ? '~' : '`';
            case KeyEvent.KEYCODE_BACKSLASH:
                return caps ? '|' : '\\';
            case KeyEvent.KEYCODE_LEFT_BRACKET:
                return caps ? '{' : '[';
            case KeyEvent.KEYCODE_RIGHT_BRACKET:
                return caps ? '}' : ']';
            case KeyEvent.KEYCODE_SEMICOLON:
                return caps ? ':' : ';';
            case KeyEvent.KEYCODE_APOSTROPHE:
                return caps ? '"' : '\'';
            case KeyEvent.KEYCODE_COMMA:
                return caps ? '<' : ',';
            case KeyEvent.KEYCODE_PERIOD:
                return caps ? '>' : '.';
            case KeyEvent.KEYCODE_SLASH:
                return caps ? '?' : '/';
            default:
                return 0;
        }
    }
}